<template>
    <div class="mod-prod-prodComm">
        <avue-crud ref="crud"
                   :data="dataList"
                   :option="tableOption"
                   :page="page"
                   :table-loading="dataListLoading"
                   @search-change="searchChange"
                   @on-load="getDataList"
                   @refresh-change="refreshChange"
                   @row-save="sourceRowSave"
                   @row-del="rowDel">
            <template slot="menuLeft">
                <el-button icon="el-icon-plus"
                           size="small"
                           type="primary"
                           @click.stop="addOrUpdateHandle()">新增
                </el-button>
                <el-button icon="el-icon-plus"
                           size="small"
                           type="primary"
                           @click.stop="getListByErp()">ERP人员
                </el-button>
              <!--
              <el-button icon="el-icon-plus"
                         size="small"
                         type="primary"
                         @click.stop="getCustListByErp()">ERP客户
              </el-button>
              <el-button icon="el-icon-plus"
                         size="small"
                         type="primary"
                         @click.stop="getSalesListByErp()">ERP销售列表
              </el-button>
              -->
            </template>
            <template slot="menu"
                      slot-scope="scope">
                <el-button
                    icon="el-icon-edit"
                    size="small"
                    type="primary"
                    @click="addOrUpdateHandle(scope.row.id)">编辑
                </el-button>
                <el-button
                    icon="el-icon-delete"
                    size="small"
                    type="danger"
                    @click="deleteHandle(scope.row.id)">删除
                </el-button>
            </template>
        </avue-crud>
        <add-or-update v-if="addOrUpdateVisible"
                       ref="addOrUpdate"
                       @refreshDataList="refreshChange"></add-or-update>
    </div>
</template>

<script>
import {tableOption} from '@/crud/prod/prodComm'
import AddOrUpdate from './cust-add-update'

export default {
	data () {
		return {
			dataList: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
                pageSize: 1000, // 每页显示多少条
                pageSizes: [1000]
			},
			dataListLoading: false,
			tableOption: tableOption,
			permission: {
				delBtn: this.isAuth('prod:prodComm:delete')
			},
			addOrUpdateVisible: false
		}
	},
	components: {
		AddOrUpdate
	},
	created () {
	},
	mounted () {
	},
	methods: {
		getDataList (page, params, done) {
			this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl('/crm/base/emp/page'),
				method: 'get',
				params: this.$http.adornParams(Object.assign({
					current: page == null ? this.page.currentPage : page.currentPage,
					size: page == null ? this.page.pageSize : page.pageSize
				}, params))
			}).then(({data}) => {
				this.dataList = data.obj.records
				this.page.total = data.obj.total
				this.dataListLoading = false
				if (done) {
					done()
				}
			})
		},
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    //从ERP获取人员信息
    getListByErp (id) {
          this.$confirm(`确定要获取人员信息吗?`, '友情提醒', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
              .then(() => {
                this.$http({
                  url: this.$http.adornUrl(`/crm/base/emp/getEmpByErp`),
                  method: 'post',
                  data: this.$http.adornData({
                    id: id
                  }, false)
                }).then(({data}) => {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.getDataList(this.page)
                    }
                  })
                })
              })
              .catch(() => {
              })
        },
    getCustListByErp (id) {
      this.$confirm(`确定要获取人员信息吗?`, '友情提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl(`/crm/base/emp/getCustListByErp`),
              method: 'post',
              data: this.$http.adornData({
                id: id
              }, false)
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.page)
                }
              })
            })
          })
          .catch(() => {
          })
    },
    //从ERP获取人员信息
    getSalesListByErp (id) {
      this.$confirm(`确定要获取人员信息吗?`, '友情提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl(`/crm/base/emp/getSalesListByErp`),
              method: 'post',
              data: this.$http.adornData({
                id: id
              }, false)
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.page)
                }
              })
            })
          })
          .catch(() => {
          })
    },
    // 删除和批量删除
    deleteHandle (id) {
            this.$confirm(`确定进行删除操作?`, '友情提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http({
                        url: this.$http.adornUrl(`/crm/base/emp/` + id),
                        method: 'delete',
                        data: this.$http.adornData({
                            id: id
                        }, false)
                    }).then(({data}) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList(this.page)
                            }
                        })
                    })
                })
                .catch(() => {
                })
        },
		rowDel: function (row, index) {
			this.$confirm('确定进行删除操作?', '友情提醒', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http({
					url: this.$http.adornUrl('/crm/base/emp/' + row.id),
          method: 'post',
					data: this.$http.adornData({})
				}).then(({data}) => {
					this.getDataList()
					this.$message({
						message: '操作成功',
						type: 'success',
						duration: 1500,
						onClose: () => {
						}
					})
				})
			}).catch(() => {
			})
		},
		sourceRowSave (form, done, loading) {
			console.log('🚀 ~ form', form)
			this.$http({
				url: this.$http.adornUrl('/crm/base/emp/saveEmp'),
				method: 'post',
				data: this.$http.adornData({
					empNo: form.empNo,
					empName: form.empName,
					deptId: form.deptId,
				})
			}).then(({data}) => {
				this.$message({
					message: '操作成功',
					type: 'success',
					duration: 1500,
					onClose: () => {
						if (done) {
							done()
						}
						this.getDataList(this.page)
					}
				})
			})
		},
		/**
		 * 刷新回调
		 */
		refreshChange () {
			this.getDataList(this.page)
		},
		searchChange (params, done) {
			this.getDataList(this.page, params, done)
		}
	}
}
</script>

<style lang="scss" scoped>
</style>
